﻿using System;
using System.Windows.Forms;
using DesignerHelper.Addons;
using DesignerHelper.Main;
using SpreadsheetGear;
using SpreadsheetGear.Commands;

namespace WorkbookDesigner.Addons.WorkbookExplorer
{
    internal sealed partial class PageSetupContainer : UserControl, IMarine<IWorksheet>
    {
        internal event SettingRequestHandler SettingCommit;
        internal event ResetRequestHandler ResetCommit;

        private ControlInitWorks NewInitWorks { get; set; }

        private IWorksheet SelectedSheet { get; set; }

        public PageSetupContainer()
        {
            InitializeComponent();
            NewInitWorks = new ControlInitWorks();
            HookEvnets();
        }

        public PageSetupContainer(IWorksheet sheet) : this()
        {
            SelectedSheet = sheet;
            TriggerInit(SelectedSheet);
        }

        public void TriggerInit(IWorksheet worksheet)
        {
            SelectedSheet = worksheet;
            TriggerReset();
        }

        private void HookEvnets()
        {
            SettingCommit += NewInitWorks.SettingCommitHandler;
            ResetCommit += NewInitWorks.ResetCommitHandler;
        }

        public void TriggerReset()
        {
            NewInitWorks.TriggerInit(SelectedSheet);
            tabControl1.TabPages.Clear();
            tabControl1.TabPages.AddRange(NewInitWorks.PageOfPageSetup());
        }

        public void TriggerSet()
        {
            NewInitWorks.TriggerSet();
        }

        private void UnhookEvents()
        {
            SettingCommit -= NewInitWorks.SettingCommitHandler;
            ResetCommit -= NewInitWorks.ResetCommitHandler;
        }

        private class ControlInitWorks
        {
            #region  Controls Instance

            private PageSetupMain Psm { get; set; }
            private Footer Ft { get; set; }
            private Header Hd { get; set; }
            private Margins Mg { get; set; }
            private Sheet St { get; set; }

            #endregion Instance Controls

            private IWorksheet SelectedSheet { get; set; }
            private IPageSetup PageSetup => SelectedSheet.PageSetup;
            private LockBookWorker LbwTrigger { get; set; }

            internal ControlInitWorks()
            {
                InitControls();
            }

            private void InitControls()
            {
                Psm = new PageSetupMain() {Dock = DockStyle.Fill,};
                Ft = new Footer() {Dock = DockStyle.Fill,};
                Hd = new Header() {Dock = DockStyle.Fill};
                Mg = new Margins() {Dock = DockStyle.Fill};
                St = new Sheet() {Dock = DockStyle.Fill};
            }

            internal void TriggerInit(IWorksheet sheet)
            {
                SelectedSheet = sheet;
                LbwTrigger = LockBookWorker.GetWorker(SelectedSheet);
                TriggerReset();
            }

            internal void TriggerSet()
            {
                LbwTrigger.DoWork(() =>
                {
                    Psm.TriggerSet();
                    Ft.TriggerSet();
                    Hd.TriggerSet();
                    St.TriggerSet();
                    Mg.TriggerSet();
                });
            }

            internal void TriggerReset()
            {
                LbwTrigger.DoWork(() =>
                {
                    Psm.TriggerInit(PageSetup);
                    Ft.TriggerInit(PageSetup);
                    Hd.TriggerInit(PageSetup);
                    Mg.TriggerInit(PageSetup);
                    St.TriggerInit(PageSetup);
                });
            }

            internal void SettingCommitHandler(object sender, EventArgs e)
            {
                TriggerSet();
            }

            internal void ResetCommitHandler(object sender, EventArgs e)
            {
                Action refresh = () =>
                {
                    Psm.TriggerReset();
                    Ft.TriggerReset();
                    Hd.TriggerReset();
                    Mg.TriggerReset();
                    St.TriggerReset();
                };
                LbwTrigger.DoWork(refresh);
            }

            internal TabPage[] PageOfPageSetup()
            {
                TabPage[] pages = {MainPage(), Margins(), Header(), Footer(), Sheet()};
                return pages;
            }

            private TabPage MainPage()
            {
                TabPage tp = new TabPage("页面") {Name = "MainPage",};
                tp.Controls.Clear();
                tp.Controls.Add(Psm);
                return tp;
            }

            private TabPage Margins()
            {
                TabPage tp = new TabPage("页边距")
                {
                    Name = "Margins"
                };
                tp.Controls.Clear();
                tp.Controls.Add(Mg);
                return tp;
            }

            private TabPage Footer()
            {
                TabPage tp = new TabPage("页脚")
                {
                    Name = "Footer"
                };
                tp.Controls.Clear();
                tp.Controls.Add(Ft);
                return tp;
            }

            private TabPage Header()
            {
                TabPage tp = new TabPage("页眉")
                {
                    Name = "Header"
                };
                tp.Controls.Clear();
                tp.Controls.Add(Hd);
                return tp;
            }

            private TabPage Sheet()
            {
                TabPage tp = new TabPage("工作表")
                {
                    Name = "Sheet"
                };
                tp.Controls.Clear();
                tp.Controls.Add(St);
                return tp;
            }
        }

        private class Cm : CommandManager
        {
            internal Cm(ISheet sheet) : base(sheet.WorkbookSet)
            {
            }

            internal Cm(IWorkbook wb) : base(wb.WorkbookSet)
            {
                ActiveBook = wb;
            }

            private IWorkbook ActiveBook { get; set; }

            public override Command CreateCommandClear(IRange range)
            {
                return base.CreateCommandClear(range);
            }

            //public Command CreateCommandClearComment(IRange range)
            //{
            //    Command command = (ActiveBook.ActiveWorksheet.Range);
            //    return command;
            //}
        }

        internal void OnSettingCommit()
        {
            var e = SettingCommit;
            e?.Invoke(this, new EventArgs());
        }

        internal void OnResetCommit()
        {
            var e = ResetCommit;
            e?.Invoke(this, new EventArgs());
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            CommandManager cm = new Cm(SelectedSheet);
            Command c = cm.CreateCommandPrint(SelectedSheet, SpreadsheetGear.Printing.PrintWhat.Sheet, true, true);
            cm.Execute(c);
        }

        private void buttonPreview_Click(object sender, EventArgs e)
        {
            CommandManager cm = new Cm(SelectedSheet);
            Command c = cm.CreateCommandPrint(SelectedSheet, SpreadsheetGear.Printing.PrintWhat.Sheet, true, true);
            cm.Execute(c);
        }

        #region interface

        void IMarine<IWorksheet>.TriggerInit(IWorksheet sendIn)
        {
            TriggerInit(sendIn);
        }

        void IScv<IWorksheet>.TriggerReset()
        {
            TriggerReset();
        }

        void IScv<IWorksheet>.TriggerSet()
        {
            TriggerSet();
        }

        Control IScv<IWorksheet>.GetControl()
        {
            return this;
        }

        #endregion
    }
}